package com.example.noteshare.Utilities;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

public class HTTP {

        private HTTP() {
        }

        private static final String USER_AGENT = "Mozilla/5.0";

        public static String formatParams(final Map<String, String> params) {
                StringBuilder sb = new StringBuilder();

                for (final Map.Entry<String, String> entry : params.entrySet()) {
                	try{
                        sb.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                        sb.append("=");
                        sb.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
                        sb.append("&");
                	}catch(UnsupportedEncodingException e){
                		e.printStackTrace();
                	}
                }

                if(sb.length() > 1){
                	sb.deleteCharAt(sb.length() - 1);
                }
                
                return sb.toString();
        }

        public static String doRequest(final RequestType type, final String url, final Map<String, String> params) {
                return type == RequestType.POST ? doPost(url, formatParams(params))
                                : doGet(url, formatParams(params));
        }

        private static String doGet(final String url, final String params) {
                StringBuilder urlBuilder = new StringBuilder();
                StringBuilder response = new StringBuilder();

                urlBuilder.append(url);
                if (!params.isEmpty()) {
                        urlBuilder.append("?").append(params);
                }
                 
                try{
                        URL obj = new URL(urlBuilder.toString());
                        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
         
                        // optional default is GET
                        con.setRequestMethod("GET");
         
                        //add request header
                        con.setRequestProperty("User-Agent", USER_AGENT);
         
                        //int responseCode = con.getResponseCode();
         
                        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                        String inputLine;
         
                        while ((inputLine = in.readLine()) != null) {
                                response.append(inputLine);
                        }
                        in.close();
                }catch(Exception e){
                        e.printStackTrace();
                }
 
                return response.toString();
                
        }

        
        private static String doPost(final String url, final String params) {
                
                StringBuilder response = new StringBuilder();

                try{
                        URL obj = new URL(url);
                        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        
                        // add reuqest header
                        con.setRequestMethod("POST");
                        con.setRequestProperty("User-Agent", USER_AGENT);
                        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        
                        // Send post request
                        con.setDoOutput(true);
                        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                        wr.writeBytes(params);
                        wr.flush();
                        wr.close();
        
                        // int responseCode = con.getResponseCode();
        
                        BufferedReader in = new BufferedReader(new InputStreamReader(
                                        con.getInputStream()));
                        String inputLine;
        
                        while ((inputLine = in.readLine()) != null) {
                                response.append(inputLine);
                        }
                        in.close();
                }catch(Exception e){
                        e.printStackTrace();
                }

                return response.toString();

        }
        
        

}